﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using VisionApp.Startup.Models;
using VM.Core;
using VM.PlatformSDKCS;
using static System.Net.Mime.MediaTypeNames;

namespace VisionApp.Startup.Services
{
    
    public class AlgoManager
    {
        public event EventHandler OnProductLoaded;
        private ProductModel currentProduct=new();
        private List<ProductModel> _ProductList = new();
        private List<AlgorithmModel> _AlgoList = new();
        private readonly CommunicationService communicationService;
        private readonly ILogger<AlgoManager> logger;

        public AlgoManager(CommunicationService communicationService, ILogger<AlgoManager> logger)
        {
            this.communicationService = communicationService;
            this.logger = logger;
            // 添加一个数据队列

            // 获取解决方案列表
            var products = Directory.GetDirectories(Constants.BaseAlgorithmPath);
            foreach (var p in products)
            {
                var sol = new ProductModel()
                {
                    Name = Path.GetFileName(p),
                    Path = p
                };
                _ProductList.Add(sol);
            }
            currentProduct = _ProductList[0];
            LoadProduct(currentProduct);// 加载一个默认产品
            this.communicationService.OnProductTypeChanged += CommunicationService_OnProductTypeChanged;
        }

        private void CommunicationService_OnProductTypeChanged(object? sender, ProductType e)
        {
            // 切换产品
            var productName = e.ProductName;
            if (productName == currentProduct.Name) return;
            var product=_ProductList.Find(p => p.Name == productName);
            if (product == null) return;
            currentProduct = product;
            System.Windows.Application.Current.Dispatcher.Invoke(() =>
            {
                LoadProduct(currentProduct);
            });
        }

        public List<ProductModel> GetProductList() => _ProductList;
        public List<AlgorithmModel> GetAlgoList() => _AlgoList;
        private void LoadProduct(ProductModel product)
        {
            try
            {
                logger?.LogInformation($"准备载入产品{product.Name}。");
                foreach (var item in _AlgoList)
                {
                    item.VmProcedure.Dispose();
                }
                _AlgoList.Clear();

                var procs = Directory.GetFiles(product.Path);
                for (int i = 0; i < procs.Length; i++)
                {
                    var proc = VmProcedure.Load(procs[i]);
                    //var proc = VmProcedure.Load(@"D:\C022CodeAndProject\vision-app-hik\VisionApp.Hik\VisionApp.Startup\bin\Release\net8.0-windows\Algorithms\LocateLogo\1-Locate1.prc");
                    var algo = new AlgorithmModel()
                    {
                        CameraName = $"Camera{i}",// 使用名字关联算法、相机、显示控件
                        VmProcedure = proc
                    };

                    _AlgoList.Add(algo);
                }
                OnProductLoaded?.Invoke(null, null);

                logger?.LogInformation($"载入产品{product.Name}成功。");
            }
            catch (VmException ex)
            {
                logger?.LogError($"加载出错：{ex.Message}");
            }
            catch (Exception ex)
            {
                logger?.LogError($"加载出错：{ex.Message}");
            }
        }
    }
}
